﻿--[[--------------------------------------------------------------------
	oUF_Tukz V090715
	
	I'd like to thank Haste for his awesome oUF framework, without which 
	this layout would do absolutely nothing. I'd also like to thank Caellian 
	for his cleanly written oUF_Caellian which helped me as a guide to write 
	this layout. 

	Supported Units:
		Player
		Pet
		Target
		Target Target
		Focus
		Party 
		Vehicule
		Raid10
		Raid25
		Raid40
		Raid15

	Required Dependencies:
		oUF
	
----------------------------------------------------------------------]]
------------------------------------------------------------------------
--	Layout Start here
------------------------------------------------------------------------

------------------------------------------------------------------------
--	Textures and Medias
------------------------------------------------------------------------

local settings = Tukz.oUF
local mediaPath = [=[Interface\Addons\oUF_Tukz\media\]=]

local floor = math.floor
local format = string.format

local epstyle
local player_name, _ = UnitName("player")
if player_name == "寶爸爸" or player_name == "Cast" then
	epstyle = 1
else
	epstyle = 0
end

--local normTex = mediaPath..[=[textures\normTex]=]
local normTex = "Interface\\AddOns\\eMedia\\blank"
local glowTex = mediaPath..[=[textures\glowTex]=]
local bubbleTex = mediaPath..[=[textures\bubbleTex]=]
local buttonTex = mediaPath..[=[textures\buttonTex]=]
local highlightTex = mediaPath..[=[textures\highlightTex]=]
local borderTex = mediaPath..[=[textures\border]=]
local blankTex = mediaPath..[=[textures\blank]=]

local backdrop = {
	bgFile = blankTex,
	insets = {top = -1, left = -1, bottom = -1, right = -1},
}

local backdrop2 = {
	bgFile = blankTex,
	edgeFile = blankTex, 
	tile = false, tileSize = 0, edgeSize = 1, 
	insets = {top = -1, left = -1, bottom = -1, right = -1},
}

function CreatePanel(height, width, x, y, anchorPoint, anchorPointRel, anchor, level, parent, strata)
	local Panel = CreateFrame("Frame", _, parent)
	Panel:SetFrameLevel(level)
	Panel:SetFrameStrata(strata)
	Panel:SetHeight(height)
	Panel:SetWidth(width)
	Panel:SetPoint(anchorPoint, anchor, anchorPointRel, x, y)
	Panel:SetBackdrop( { 
	  bgFile = BLANK_TEXTURE, 
	  edgeFile = BLANK_TEXTURE, 
	  tile = false, tileSize = 0, edgeSize = 1, 
	  insets = { left = 1, right = 1, top = 1, bottom = 1 }
	})
	--Panel:SetBackdropColor(0.41, 0.8, 0.94, 1)
	Panel:SetBackdropColor(58/225, 102/255, 100/255, 1)
	Panel:SetBackdropBorderColor(0, 0, 0, 1)
	Panel:EnableMouse(true)
	Panel:Show()
	return Panel
end 

local font = settings.font
local fontn = "Interface\\AddOns\\eMedia\\font.ttf"

local _, class = UnitClass("player")

local totdebuffs = settings.totdebuffs
local totot = settings.totot
local focustarget = settings.focustarget
local unitcastbar = settings.unitcastbar
local targetmanatext = settings.targetmanatext
local charportrait = settings.charportrait
local cblatency = settings.cblatency
local auratimer = settings.auratimer
local lowThreshold = settings.lowThreshold
local highThreshold = settings.highThreshold

------------------------------------------------------------------------
--	Colors
------------------------------------------------------------------------

local colors = setmetatable({
	power = setmetatable({
		["MANA"] = {0.31, 0.45, 0.63},
		["RAGE"] = {0.69, 0.31, 0.31},
		["FOCUS"] = {0.71, 0.43, 0.27},
		["ENERGY"] = {0.65, 0.63, 0.35},
		["RUNES"] = {0.55, 0.57, 0.61},
		["RUNIC_POWER"] = {0, 0.82, 1},
		["AMMOSLOT"] = {0.8, 0.6, 0},
		["FUEL"] = {0, 0.55, 0.5},
		["POWER_TYPE_STEAM"] = {0.55, 0.57, 0.61},
		["POWER_TYPE_PYRITE"] = {0.60, 0.09, 0.17},
	}, {__index = oUF.colors.power}),
	happiness = setmetatable({
		[1] = {.69,.31,.31},
		[2] = {.65,.63,.35},
		[3] = {.33,.59,.33},
	}, {__index = oUF.colors.happiness}),
	runes = setmetatable({
		[1] = {0.69, 0.31, 0.31},
		[2] = {0.33, 0.59, 0.33},
		[3] = {0.31, 0.45, 0.63},
		[4] = {0.84, 0.75, 0.65},
	}, {__index = oUF.colors.runes}),
}, {__index = oUF.colors})

oUF.colors.tapped = {0.55, 0.57, 0.61}
oUF.colors.disconnected = {0.3, 0.3, 0.3}

oUF.colors.smooth = {0.69, 0.31, 0.31, 0.65, 0.63, 0.35, 0.15, 0.15, 0.15}

------------------------------------------------------------------------
--	Fonction (don't edit this if you don't know what you are doing!)
------------------------------------------------------------------------

local SetUpAnimGroup = function(self)
	self.anim = self:CreateAnimationGroup("Flash")
	self.anim.fadein = self.anim:CreateAnimation("ALPHA", "FadeIn")
	self.anim.fadein:SetChange(1)
	self.anim.fadein:SetOrder(2)

	self.anim.fadeout = self.anim:CreateAnimation("ALPHA", "FadeOut")
	self.anim.fadeout:SetChange(-1)
	self.anim.fadeout:SetOrder(1)
end

local Flash = function(self, duration)
	if not self.anim then
		SetUpAnimGroup(self)
	end

	self.anim.fadein:SetDuration(duration)
	self.anim.fadeout:SetDuration(duration)
	self.anim:Play()
end

local StopFlash = function(self)
	if self.anim then
		self.anim:Finish()
	end
end

local Menu = function(self)
	local unit = self.unit:gsub("(.)", string.upper, 1) 
	if _G[unit.."FrameDropDown"] then
		ToggleDropDownMenu(1, nil, _G[unit.."FrameDropDown"], "cursor")
	elseif (self.unit:match("party")) then
		ToggleDropDownMenu(1, nil, _G["PartyMemberFrame"..self.id.."DropDown"], "cursor")
	else
		FriendsDropDown.unit = self.unit
		FriendsDropDown.id = self.id
		FriendsDropDown.initialize = RaidFrameDropDown_Initialize
		ToggleDropDownMenu(1, nil, FriendsDropDown, "cursor")
	end
end

local SetFontString = function(parent, fontName, fontHeight, fontStyle)
	local fs = parent:CreateFontString(nil, "OVERLAY")
	fs:SetFont(fontName, fontHeight, fontStyle)
	fs:SetJustifyH("LEFT")
	fs:SetShadowColor(0, 0, 0)
	fs:SetShadowOffset(1.25, -1.25)
	return fs
end

local ShortValue = function(value)
	if value >= 1e6 then
		return ("%.1fm"):format(value / 1e6):gsub("%.?0+([km])$", "%1")
	elseif value >= 1e3 or value <= -1e3 then
		return ("%.1fk"):format(value / 1e3):gsub("%.?0+([km])$", "%1")
	else
		return value
	end
end

local PostUpdateHealth = function(self, event, unit, bar, min, max)
	if not UnitIsConnected(unit) then
		bar:SetValue(0)
		bar.value:SetText("|cffD7BEA5".."Offline".."|r")
	elseif UnitIsDead(unit) then
		bar.value:SetText("|cffD7BEA5".."Dead".."|r")
	elseif UnitIsGhost(unit) then
		bar.value:SetText("|cffD7BEA5".."Ghost".."|r")
	else
		if min ~= max then
			local r, g, b
			r, g, b = oUF.ColorGradient(min/max, 0.69, 0.31, 0.31, 0.65, 0.63, 0.35, 0.33, 0.59, 0.33)
			if unit == "player" and self:GetAttribute("normalUnit") ~= "pet" then
				bar.value:SetFormattedText("|cffAF5050%d|r |cffD7BEA5-|r |cff%02x%02x%02x%d%%|r", min, r * 255, g * 255, b * 255, floor(min / max * 100))
			elseif unit == "target" then
				bar.value:SetFormattedText("|cffAF5050%s|r |cffD7BEA5-|r |cff%02x%02x%02x%d%%|r", ShortValue(min), r * 255, g * 255, b * 255, floor(min / max * 100))
			else
				bar.value:SetFormattedText("|cff%02x%02x%02x%d%%|r", r * 255, g * 255, b * 255, floor(min / max * 100))
			end
		else
			if unit ~= "player" and unit ~= "pet" then
				bar.value:SetText("|cff559655"..ShortValue(max).."|r")
			else
				bar.value:SetText("|cff559655"..max.."|r")
			end
		end
	end
end

local PostNamePosition = function(self)
	self.Info:ClearAllPoints()
	if self.Power.value:GetText() and UnitIsEnemy("player", "target") and targetmanatext == true then
		self.Info:SetPoint("CENTER", 0, 0)
	else
		self.Power.value:SetAlpha(0)
		self.Info:SetPoint("LEFT", 4, 0)
	end
end

local PreUpdatePower = function(self, event, unit)
	if(self.unit ~= unit) then return end
	local _, pType = UnitPowerType(unit)
	
	local color = self.colors.power[pType]
	if color then
		self.Power:SetStatusBarColor(color[1], color[2], color[3])
	end
end

local PostUpdatePower = function(self, event, unit, bar, min, max)
	if self.unit ~= "player" and self.unit ~= "pet" and self.unit ~= "target" then return end

	local pType, pToken = UnitPowerType(unit)
	local color = colors.power[pToken]

	if color then
		bar.value:SetTextColor(color[1], color[2], color[3])
	end

	if min == 0 then
		bar.value:SetText()
	elseif not UnitIsPlayer(unit) and not UnitPlayerControlled(unit) or not UnitIsConnected(unit) then
		bar.value:SetText()
	elseif UnitIsDead(unit) or UnitIsGhost(unit) then
		bar.value:SetText()
	elseif min == max and (pType == 2 or pType == 3 and pToken ~= "POWER_TYPE_PYRITE") then
		bar.value:SetText()
	else
		if min ~= max then
			if pType == 0 then
				if unit == "target" then
					bar.value:SetFormattedText("%d%% |cffD7BEA5-|r %s", floor(min / max * 100), ShortValue(max - (max - min)))
				elseif unit == "player" and self:GetAttribute("normalUnit") == "pet" or unit == "pet" then
					bar.value:SetFormattedText("%d%%", floor(min / max * 100))
				else
					bar.value:SetFormattedText("%d%% |cffD7BEA5-|r %d", floor(min / max * 100), max - (max - min))
				end
			else
				bar.value:SetText(max - (max - min))
			end
		else
			if unit == "pet" or unit == "target" then
				bar.value:SetText(ShortValue(min))
			else
				bar.value:SetText(min)
			end
		end
	end
	if self.Info then
		if self.unit == "target" then PostNamePosition(self) end
	end
end

do
	local f = CreateFrame("Frame")
	local entering

	f:RegisterEvent("UNIT_ENTERED_VEHICLE")
	f:RegisterEvent("UNIT_EXITED_VEHICLE")
	
	local delay = 0.5
	local OnUpdate = function(self, elapsed)
		self.elapsed = (self.elapsed or delay) - elapsed
		if self.elapsed <= 0 then
			local petframe = oUF_Tukz_pet
			petframe:PLAYER_ENTERING_WORLD()
			self:SetScript("OnUpdate", nil)
			if entering and petframe.PostEnterVehicle then
				petframe:PostEnterVehicle("enter")
			elseif not entering and petframe.PostExitVehicle then
				petframe:PostExitVehicle("exit")
			end
		end
	end

	f:SetScript("OnEvent", function(self, event, unit)
		if unit == "player" then
			if event == "UNIT_ENTERED_VEHICLE" then
				entering = true
			else
				entering = false
			end
			f.elapsed = delay
			f:SetScript("OnUpdate", OnUpdate)
		end
	end)
end

local EnterVehicle = function(self, event)
	if event == "enter" then
		self.Info:Hide()
	end
end

local ExitVehicle = function(self, event)
	if event == "exit" then
		self.Info:Show()
	end
end

local delay = 0
local viperAspectName = GetSpellInfo(34074)
local UpdateManaLevel = function(self, elapsed)
	delay = delay + elapsed
	if self.parent.unit ~= "player" or delay < 0.2 or UnitIsDeadOrGhost("player") or UnitPowerType("player") ~= 0 then return end
	delay = 0

	local percMana = UnitMana("player") / UnitManaMax("player") * 100

	if AotV then
		local viper = UnitBuff("player", viperAspectName)
		if percMana >= highThreshold and viper then
			self.ManaLevel:SetText("|cffaf5050GO HAWK|r")
			Flash(self, 0.3)
		elseif percMana <= lowThreshold and not viper then
			self.ManaLevel:SetText("|cffaf5050GO VIPER|r")
			Flash(self, 0.3)
		else
			self.ManaLevel:SetText()
			StopFlash(self)
		end
	else
		if percMana <= lowThreshold then
			self.ManaLevel:SetText("|cffaf5050LOW MANA|r")
			Flash(self, 0.3)
		else
			self.ManaLevel:SetText()
			StopFlash(self)
		end
	end
end

local UpdateDruidMana = function(self)
	if self.unit ~= "player" then return end

	local num, str = UnitPowerType("player")
	if num ~= 0 then
		local min = UnitPower("player", 0)
		local max = UnitPowerMax("player", 0)

		local percMana = min / max * 100
		if percMana <= lowThreshold then
			self.FlashInfo.ManaLevel:SetText("|cffaf5050LOW MANA|r")
			Flash(self.FlashInfo, 0.3)
		else
			self.FlashInfo.ManaLevel:SetText()
			StopFlash(self.FlashInfo)
		end

		if min ~= max then
			if self.Power.value:GetText() then
				self.DruidMana:SetPoint("LEFT", self.Power.value, "RIGHT", 1, 0)
				self.DruidMana:SetFormattedText("|cffD7BEA5-|r %d%%|r", floor(min / max * 100))
			else
				self.DruidMana:SetPoint("LEFT", 4, 0)
				self.DruidMana:SetFormattedText("%d%%", floor(min / max * 100))
			end
		else
			self.DruidMana:SetText()
		end

		self.DruidMana:SetAlpha(1)
	else
		self.DruidMana:SetAlpha(0)
	end
end

local UpdateCPoints = function(self, event, unit)
	if unit == PlayerFrame.unit and unit ~= self.CPoints.unit then
	--if oUF_Tukz_player.unit == unit or PlayerFrame.unit == unit then
		self.CPoints.unit = unit
	end
end

local FormatCastbarTime = function(self, duration)
	if self.channeling then
		self.Time:SetFormattedText("%.1f ", duration)
	elseif self.casting then
		self.Time:SetFormattedText("%.1f ", self.max - duration)
	end
end

local FormatTime = function(s)
	local day, hour, minute = 86400, 3600, 60
	if s >= day then
		return format("%dd", floor(s/day + 0.5)), s % day
	elseif s >= hour then
		return format("%dh", floor(s/hour + 0.5)), s % hour
	elseif s >= minute then
		if s <= minute * 1 then
			return format('%d:%02d', floor(s/60), s % minute), s - floor(s)
		end
		return format("%dm", floor(s/minute + 0.5)), s % minute
	elseif s >= minute / 12 then
		return floor(s + 0.5), s - floor(s)
	end
	return format("%.1f", s), (s * 100 - floor(s * 100))/100
end

local CreateAuraTimer = function(self,elapsed)
	if self.timeLeft then
		self.elapsed = (self.elapsed or 0) + elapsed
		if self.elapsed >= 0.1 then
			if not self.first then
				self.timeLeft = self.timeLeft - self.elapsed
			else
				self.timeLeft = self.timeLeft - GetTime()
				self.first = false
			end
			if self.timeLeft > 0 then
				local time = FormatTime(self.timeLeft)
--				if type(time) == "string" or time >= 10 then
					self.remaining:SetText(time)
--				else
--					self.remaining:SetFormattedText("%.1f", time)
--				end
				self.remaining:SetTextColor(0.84, 0.75, 0.65)
			else
				self.remaining:Hide()
				self:SetScript("OnUpdate", nil)
			end
			self.elapsed = 0
		end
	end
end

local HideAura = function(self)
	if self.unit == "player" then
		if settings.noPlayerAuras then
			self.Buffs:Hide()
			self.Debuffs:Hide()
		else
			BuffFrame:Hide()
			TemporaryEnchantFrame:Hide()
		end
	elseif self.unit == "pet" and settings.noPetAuras then
		self.Auras:Hide()
	elseif self.unit == "target" and settings.noTargetAuras then
		self.Buffs:Hide()
		self.Debuffs:Hide()
	end
end

local CancelAura = function(self, button)
	if button == "RightButton" and not self.debuff then
		CancelUnitBuff("player", self:GetID())
	end
end

local CreateAura = function(self, button, icons)
	button.backdrop = CreateFrame("Frame", nil, button)
	button.backdrop:SetPoint("TOPLEFT", button, "TOPLEFT", -3.5, 3)
	button.backdrop:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", 4, -3.5)
	button.backdrop:SetFrameStrata("BACKGROUND")
	button.backdrop:SetBackdrop {
		edgeFile = glowTex, edgeSize = 5,
		insets = {left = 3, right = 3, top = 3, bottom = 3}
	}
	button.backdrop:SetBackdropColor(0, 0, 0, 0)
	button.backdrop:SetBackdropBorderColor(0, 0, 0)

	button.count:SetPoint("BOTTOMRIGHT", -1, 2)
	button.count:SetJustifyH("RIGHT")
	button.count:SetFont(fontn, 12, "OUTLINE")
	button.count:SetTextColor(0.84, 0.75, 0.65)

	button.cd.noOCC = true
	button.cd.noCooldownCount = true
	icons.disableCooldown = true

	button.overlay:SetTexture(buttonTex)
	button.overlay:SetPoint("TOPLEFT", button, "TOPLEFT", -2, 2)
	button.overlay:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", 2, -2)
	button.overlay:SetTexCoord(0, 1, 0.02, 1)
	button.overlay.Hide = function(self) end

	if icons ~= self.Enchant then
		button.remaining = SetFontString(button, fontn, 12, "OUTLINE")
		if self.unit == "player" then
			button:SetScript("OnMouseUp", CancelAura)
		end
	else
		button.remaining = SetFontString(button, fontn, 12, "OUTLINE")
		button.overlay:SetVertexColor(0.33, 0.59, 0.33)
	end
	button.remaining:SetPoint("TOPLEFT", 1, -1)
end

local CreateEnchantTimer = function(self, icons)
	for i = 1, 2 do
		local icon = icons[i]
		if icon.expTime then
			icon.timeLeft = icon.expTime - GetTime()
			icon.remaining:Show()
		else
			icon.remaining:Hide()
		end
		icon:SetScript("OnUpdate", CreateAuraTimer)
	end
end

local UpdateAura = function(self, icons, unit, icon, index)
	local _, _, _, _, _, duration, expirationTime, unitCaster, _ = UnitAura(unit, index, icon.filter)
	if unitCaster == "player" or unitCaster == "pet" or unitCaster == "vehicle" then
		if icon.debuff then
			icon.overlay:SetVertexColor(0.69, 0.31, 0.31)
		else
			icon.overlay:SetVertexColor(1, 1, 1)
		end
	else
		if UnitIsEnemy("player", unit) then
			if icon.debuff then
				icon.icon:SetDesaturated(true)
			end
		end
		icon.overlay:SetVertexColor(1, 1, 1)
	end

	if duration and duration > 0 then
		if auratimer == true then
		icon.remaining:Show()
		else
		icon.remaining:Hide()
		end
	else
		icon.remaining:Hide()
	end

	icon.duration = duration
	icon.timeLeft = expirationTime
	icon.first = true
	icon:SetScript("OnUpdate", CreateAuraTimer)
end

local HidePortrait = function(self, unit)
	if self.unit == "target" then
		if not UnitExists(self.unit) or not UnitIsConnected(self.unit) or not UnitIsVisible(self.unit) then
			self.Portrait:SetAlpha(0)
		else
			self.Portrait:SetAlpha(0.2)
		end
	end
end

local PostUpdateThreat = function(self, event, unit, status)
	if not status or status == 0 then
		self.ThreatFeedbackFrame:SetBackdropBorderColor(0, 0, 0)
		self.ThreatFeedbackFrame:Show()
	end
end

------------------------------------------------------------------------
--	Layout Style
------------------------------------------------------------------------

------------------------------------------------------------------------
--	Health and Power and LuaTexts
------------------------------------------------------------------------

local SetStyle = function(self, unit)
	self.menu = Menu
	self.colors = colors
	self:RegisterForClicks("AnyUp")
	self:SetAttribute("type2", "menu")

	self:SetScript("OnEnter", UnitFrame_OnEnter)
	self:SetScript("OnLeave", UnitFrame_OnLeave)

	self:SetBackdrop(backdrop)
	self:SetBackdropColor(0, 0, 0)
	self.MoveableFrames = true
	self.Health = CreateFrame("StatusBar", self:GetName().."_Health", self)
	if unit then
		self.Health:SetFrameLevel(2)
	elseif self:GetAttribute("unitsuffix") then
		self.Health:SetFrameLevel(2)
	elseif not unit then
		self.Health:SetFrameLevel(2)
	end
	
	self.Health:SetHeight((unit == "player" or unit == "target") and 25 
	or self:GetParent():GetName():match("oUF_Party") and 37
		or (unit == "focus" or unit == "focustarget") and 18
		or (unit == "targettarget" or unit == "pet") and 18
		or (unit == "targettargettarget") and 12
		or self:GetAttribute("unitsuffix") == "pet" and 10 or 16)
	
	self.Health:SetPoint("TOPLEFT")
	self.Health:SetPoint("TOPRIGHT")
	self.Health:SetStatusBarTexture(normTex)

	self.Health.colorTapping = true
	self.Health.colorDisconnected = true
	self.Health.colorSmooth = true
	--self.Health.colorReaction = true
	self.Health.frequentUpdates = true
	--self.Health.Smooth = true
	--self.Health.colorClassPet = false  
	--self.Health.colorClass = true
	
	self.Health.bg = self.Health:CreateTexture(nil, "BORDER")
	self.Health.bg:SetAllPoints(self.Health)
	self.Health.bg:SetTexture(normTex)
    self.Health.bg:SetAlpha(1)
	self.Health.bg.multiplier = 0.3


	
	self.Health.value = SetFontString(self.Health, font,(unit == "player" or unit == "target") and 12 or 12)
	if unit == "player" or unit == "target" then
		self.Health.value:SetPoint("RIGHT", -4, -0)
	else
		self.Health.value:Hide()
	end

    

	if unit ~= "player" then
		self.Info = SetFontString(self.Health, font, unit == "target" and 12 or 12)
		if unit == "target" then
			self.Info:SetPoint("LEFT", 1, 0)
			self:Tag(self.Info, "[GetNameColor][NameLong] [DiffColor][level] [shortclassification]")
		elseif unit == "targettarget" or unit == "targettargettarget" or unit == "pet" then
			self.Info = SetFontString(self.Health, font, unit == "targettarget" and 11 or 11)
			self.Info:SetPoint("CENTER", 0, 0)
			self:Tag(self.Info, "[GetNameColor][NameMedium]")
		else
			self.Info:SetPoint("CENTER", 0, 0)
			self:Tag(self.Info, "[GetNameColor][NameMedium]")
		end
	end

	if not (self:GetAttribute("unitsuffix") == "pet") then
		self.Power = CreateFrame("StatusBar", self:GetName().."_Power", self)
		self.Power:SetHeight((unit == "player" or unit == "target") and 10 or 0)
		self.Power:SetWidth((unit == "player" or unit == "target") and 220 or 110)
		self.Power:SetPoint("TOP", self.Health, "BOTTOM", 0, 0)
		self.Power:SetStatusBarTexture(normTex)
		self.Power:SetFrameStrata("BACKGROUND")
		self.Power:SetFrameLevel(1)

		self.Power.colorTapping = true
		self.Power.colorDisconnected = true
		self.Power.colorPower = true
		self.Power.colorClass = true
		self.Power.colorReaction = true

		self.Power.frequentUpdates = true
		--self.Power.Smooth = true

		self.Power.bg = self.Power:CreateTexture(nil, "BORDER")
		self.Power.bg:SetAllPoints(self.Power)
		self.Power.bg:SetTexture(normTex)
		self.Power.bg:SetAlpha(1)
		self.Power.bg.multiplier = 0.4
		
		self.Power.bg2 = CreateFrame("Frame", nil, self)
		self.Power.bg2:SetPoint("TOPLEFT", self.Power.bg, "TOPLEFT", -4, 4)
		self.Power.bg2:SetPoint("BOTTOMRIGHT", self.Power.bg, "BOTTOMRIGHT", 4, -4)
		self.Power.bg2:SetFrameStrata("BACKGROUND")
		self.Power.bg2:SetFrameLevel(0)
		self.Power.bg2:SetBackdrop {
			bgFile = normTex,
			edgeFile = glowTex, edgeSize = 5,
			insets = {left = 3, right = 3, top = 3, bottom = 3}
			}
		self.Power.bg2:SetBackdropColor(0, 0, 0, 1)
		self.Power.bg2:SetBackdropBorderColor(0, 0, 0)
		self.ThreatFeedbackFrame = self.Power.bg2


		self.Power.value = SetFontString(self.Health, font, (unit == "player" or unit == "target") and 12 or 12)
			if (unit == "player") or (unit == "target" and targetmanatext == true) then
				self.Power.value:SetPoint("LEFT", 4, 0)
			else
				self.Power.value:Hide()
			end
	end

	if unit == "player" then
		self.Combat = self.Health:CreateTexture(nil, "OVERLAY")
		self.Combat:SetHeight(24)
		self.Combat:SetWidth(24)
		self.Combat:SetPoint("CENTER")
		self.Combat:SetVertexColor(0.69, 0.31, 0.31)

		self.FlashInfo = CreateFrame("Frame", "FlashInfo", self)
		self.FlashInfo:SetScript("OnUpdate", UpdateManaLevel)
		self.FlashInfo.parent = self
		self.FlashInfo:SetToplevel(true)
		self.FlashInfo:SetAllPoints(self.Health)

		self.FlashInfo.ManaLevel = SetFontString(self.FlashInfo, font, 12)
		self.FlashInfo.ManaLevel:SetPoint("CENTER", 0, 0)

------------------------------------------------------------------------
--	Runes and Totembar
------------------------------------------------------------------------	
		if class == 'SHAMAN' then
			self.TotemBar = {}
			self.TotemBar.destroy = true
			for i = 1, 4 do
				self.TotemBar[i] = CreateFrame('StatusBar', self:GetName().."_TotemBar"..i, self.Health)
				self.TotemBar[i]:SetHeight(4)
				self.TotemBar[i]:SetWidth(240/4 - 0.75)
				if i == 1 then
					self.TotemBar[i]:SetPoint('BOTTOMLEFT', self.Health, 'BOTTOMLEFT', 0, 21)
				else
					self.TotemBar[i]:SetPoint('TOPLEFT', self.TotemBar[i-1], 'TOPRIGHT', 1, 0)
				end
				
				self.TotemBar[i]:SetStatusBarTexture(normTex)
				self.TotemBar[i]:SetBackdrop(backdrop)
				self.TotemBar[i]:SetBackdropColor(0, 0, 0)
				self.TotemBar[i]:SetMinMaxValues(0, 1)
				
				self.TotemBar[i].bg = self.TotemBar[i]:CreateTexture(nil, 'BORDER')
				self.TotemBar[i].bg:SetAllPoints(self.TotemBar[i])
				self.TotemBar[i].bg:SetTexture(normTex)
				self.TotemBar[i].bg:SetVertexColor(0.1, 0.1, 0.1)
				--self.TotemBar[i].bg.multiplier = .3
			end
		end
		
		if class == "DEATHKNIGHT" then
			self.RuneBar = {}
			for i = 1, 6 do
				self.RuneBar[i] = CreateFrame("StatusBar", self:GetName().."_RuneBar"..i, self.Health)
				self.RuneBar[i]:SetHeight(4)
				self.RuneBar[i]:SetWidth(245/6)
				if (i == 1) then
					self.RuneBar[i]:SetPoint("BOTTOMLEFT", self.Health, "BOTTOMLEFT", 0, 21)
				else
					self.RuneBar[i]:SetPoint("TOPLEFT", self.RuneBar[i-1], "TOPRIGHT", 1, 0)
				end
				self.RuneBar[i]:SetStatusBarTexture(normTex)
				self.RuneBar[i]:SetBackdrop(backdrop)
				self.RuneBar[i]:SetBackdropColor(0, 0, 0)
				self.RuneBar[i]:SetMinMaxValues(0, 1)

				self.RuneBar[i].bg = self.RuneBar[i]:CreateTexture(nil, "BORDER")
				self.RuneBar[i].bg:SetAllPoints(self.RuneBar[i])
				self.RuneBar[i].bg:SetTexture(Tex)
				self.RuneBar[i].bg:SetVertexColor(0.1, 0.1, 0.1)
			end
		end

------------------------------------------------------------------------
--	Extra condition (druid mana in cat and bear form)
------------------------------------------------------------------------

		if class == "DRUID" then
			CreateFrame("Frame"):SetScript("OnUpdate", function() UpdateDruidMana(self) end)
			self.DruidMana = SetFontString(self.Health, font, 12)
			self.DruidMana:SetTextColor(1, 0.49, 0.04)
		end
	end

------------------------------------------------------------------------
--	Vehicule Swap
------------------------------------------------------------------------

	if unit == "pet" then
		self.PostEnterVehicle = EnterVehicle
		self.PostExitVehicle = ExitVehicle
	end

------------------------------------------------------------------------
--	Auras
------------------------------------------------------------------------	
	
	if unit == "player" or unit == "target" or unit == "targettarget" or unit == "focus" then
		self.Debuffs = CreateFrame("Frame", nil, self)
		self.Debuffs:SetHeight(26)
		self.Debuffs:SetWidth(250)
		self.Debuffs.size = 26
		self.Debuffs.spacing = 2

		self.Buffs = CreateFrame("Frame", nil, self)
		self.Buffs:SetHeight(26)
		self.Buffs:SetWidth(250)
		self.Buffs.size = 26
		self.Buffs.spacing = 2
		self.Buffs.num = 9
		self.Buffs.numBuffs = 9
		self.Buffs.numDebuffs = 18
		
			if unit == "player" then
				self.Buffs:SetPoint("TOPLEFT", self, "TOPLEFT", -0.5, 30)
				self.Buffs.initialAnchor = "TOPLEFT"
				self.Buffs["growth-y"] = "UP"	
				self.Debuffs:SetPoint("TOPLEFT", self, "TOPLEFT", -0.5, 60)
				self.Debuffs.initialAnchor = "TOPRIGHT"
				self.Debuffs["growth-y"] = "UP"
				self.Debuffs["growth-x"] = "LEFT"
			end
			if unit == "target" then
				if epstyle == 1 then
					self.Auras = CreateFrame('Frame', nil, self)
					self.Auras:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', 160, -20)
					self.Auras:SetHeight(30 * 4)
					self.Auras:SetWidth(420)
					self.Auras.size = 30
					self.Auras.spacing = 5
					self.Auras.initialAnchor = 'TOPLEFT'
					self.Auras['growth-x'] = 'RIGHT'
					self.Auras['growth-y'] = 'DOWN'
					self.PostCreateAuraIcon = createAura
				else
					self.Buffs:SetPoint("TOPLEFT", self, "TOPLEFT", -0.5, 50)
					self.Buffs.initialAnchor = "TOPLEFT"
					self.Buffs["growth-y"] = "UP"
					self.Debuffs:SetPoint("TOPLEFT", self, "TOPLEFT", -0.5, 80)
					self.Debuffs.initialAnchor = "TOPRIGHT"
					self.Debuffs["growth-y"] = "UP"
					self.Debuffs["growth-x"] = "LEFT"
					self.Debuffs.onlyShowPlayer = false
				end
	
				self.CPoints = { }
				self.CPoints.unit = PlayerFrame.unit
				for i=1, MAX_COMBO_POINTS do
					if i == 1 then
						self.CPoints[i] =  CreatePanel(6, 40, 14, -5, "TOPLEFT", "BOTTOMLEFT", self, 2, self, "background")
					else
						self.CPoints[i] =  CreatePanel(6, 40, 5.5, 0, "LEFT", "RIGHT", self.CPoints[i-1], 2, self, "background")
					end
					--self.CPoints:Hide()
				end
				self:RegisterEvent("UNIT_COMBO_POINTS", UpdateCPoints)
			end
			if unit == "targettarget" and totdebuffs == true then
				self.Debuffs:SetHeight(23.5)
				self.Debuffs:SetWidth(23.5 * 4)
				self.Debuffs.size = 23.5
				self.Debuffs.spacing = 2
				self.Debuffs.num = 4
				self.Debuffs.numDebuffs = 4
							
				self.Debuffs:SetPoint("BOTTOMLEFT", self, "TOPLEFT", 0, 2)
				self.Debuffs.initialAnchor = "BOTTOMLEFT"
				self.Debuffs["growth-x"] = "RIGHT"
				self.Debuffs["growth-y"] = "UP"
			end
			if (unit == "player" or unit == "target") and (charportrait == true) then
				self.Portrait = CreateFrame("PlayerModel", nil, self)
				self.Portrait:SetFrameLevel(10)
				self.Portrait:SetHeight(27)
				self.Portrait:SetAlpha(0.3)
				self.Portrait:SetPoint("TOPLEFT", self.Power, "BOTTOMLEFT", 0, 36)
				self.Portrait:SetPoint("TOPRIGHT", self.Power, "BOTTOMRIGHT", 0, 36)
				table.insert(self.__elements, HidePortrait)
			end

------------------------------------------------------------------------
--	Combat text over HealthBar
------------------------------------------------------------------------	

		self.CombatFeedbackText = SetFontString(self.Health, font, 18, "OUTLINE")
		self.CombatFeedbackText:SetPoint("CENTER", 0, 1)
		self.CombatFeedbackText.colors = {
			DAMAGE = {0.69, 0.31, 0.31},
			CRUSHING = {0.69, 0.31, 0.31},
			CRITICAL = {0.69, 0.31, 0.31},
			GLANCING = {0.69, 0.31, 0.31},
			STANDARD = {0.84, 0.75, 0.65},
			IMMUNE = {0.84, 0.75, 0.65},
			ABSORB = {0.84, 0.75, 0.65},
			BLOCK = {0.84, 0.75, 0.65},
			RESIST = {0.84, 0.75, 0.65},
			MISS = {0.84, 0.75, 0.65},
			HEAL = {0.33, 0.59, 0.33},
			CRITHEAL = {0.33, 0.59, 0.33},
			ENERGIZE = {0.31, 0.45, 0.63},
			CRITENERGIZE = {0.31, 0.45, 0.63},
		}

	end

------------------------------------------------------------------------
--	Castbar
------------------------------------------------------------------------	

	--if not (self:GetAttribute("unitsuffix") == "pet") and (unitcastbar == true) then
	if (unitcastbar == true) then
		self.Castbar = CreateFrame("StatusBar", self:GetName().."_Castbar", self)
		self.Castbar:SetStatusBarTexture(normTex)
		self.Castbar:SetStatusBarColor(0.31, 0.45, 0.63, 0.5)

		self.Castbar.bg = self.Castbar:CreateTexture(nil, "BORDER")
		self.Castbar.bg:SetAllPoints(self.Castbar)
		self.Castbar.bg:SetTexture(normTex)
		self.Castbar.bg:SetVertexColor(0.15, 0.15, 0.15)
		
		self.FrameBackdrop = CreateFrame("Frame", nil, self)
		self.FrameBackdrop:SetParent(self.Castbar)
		self.FrameBackdrop:SetPoint("TOPLEFT", self.Castbar, "TOPLEFT", -4, 4)
		self.FrameBackdrop:SetPoint("BOTTOMRIGHT", self.Castbar, "BOTTOMRIGHT", 4, -4)
		self.FrameBackdrop:SetFrameStrata("BACKGROUND")
		self.FrameBackdrop:SetBackdrop {
			bgFile = normTex,
			edgeFile = glowTex, edgeSize = 5,
			insets = {left = 3, right = 3, top = 3, bottom = 3}
		}
		self.FrameBackdrop:SetFrameLevel(1)
		self.FrameBackdrop:SetBackdropColor(0, 0, 0, 1)
		self.FrameBackdrop:SetBackdropBorderColor(0, 0, 0)
	
	local cfg = settings.coords
		if unit == "player" then
			self.Castbar:SetFrameLevel(7)
			self.Castbar:SetHeight(18)
			self.Castbar:SetPoint("BOTTOMLEFT", self.Power, "TOPLEFT", 0, 26)
			self.Castbar:SetPoint("BOTTOMRIGHT", self.Power, "TOPRIGHT", 0, 26)
		elseif unit == "target" then
			self.Castbar:SetFrameLevel(7)
			self.Castbar:SetHeight(18)
			self.Castbar:SetPoint("BOTTOMLEFT", self.Power, "TOPLEFT", 0, 26)
			self.Castbar:SetPoint("BOTTOMRIGHT", self.Power, "TOPRIGHT", 0, 26)
		elseif unit == "focus" then
			self.Castbar:SetFrameLevel(6)
			self.Castbar:SetHeight(10)
			self.Castbar:SetWidth(110)
			self.Castbar:SetPoint("BOTTOM", self, "TOP", 0, 1)
		elseif unit == "pet" then
			self.Castbar:SetFrameLevel(6)
			self.Castbar:SetHeight(10)
			self.Castbar:SetWidth(110)
			self.Castbar:SetPoint("BOTTOM", self, "TOP", 0, 1)
		else
			self.Castbar:Hide()
		end

		if unit == "player" or unit == "target" or unit == "focus" then
			self.Castbar.Time = SetFontString(self.Castbar, font, 12)
			self.Castbar.Time:SetPoint("RIGHT", -2, 1)
			self.Castbar.Time:SetTextColor(0.84, 0.75, 0.65)
			self.Castbar.Time:SetJustifyH("RIGHT")
			self.Castbar.CustomTimeText = FormatCastbarTime

			self.Castbar.Text = SetFontString(self.Castbar, font, 12)
			self.Castbar.Text:SetPoint("LEFT", 3, 1)
			self.Castbar.Text:SetPoint("RIGHT", self.Castbar.Time, "LEFT", -2, 2)
			self.Castbar.Text:SetTextColor(0.84, 0.75, 0.65)

			--[[
			self.Castbar.Icon = self.Castbar:CreateTexture(nil, "ARTWORK")
			self.Castbar.Icon:SetHeight(28.5)
			self.Castbar.Icon:SetWidth(28.5)
			self.Castbar.Icon:SetTexCoord(0, 1, 0, 1)
			if unit == "player" then
				self.Castbar.Icon:SetPoint("BOTTOMRIGHT", self.Castbar, "BOTTOMLEFT", -5, 0)
			elseif unit == "target" then
				self.Castbar.Icon:SetPoint("BOTTOMLEFT", self.Castbar, "BOTTOMRIGHT", 5, 0)
			elseif unit == "focus" then
				self.Castbar.Icon:SetPoint("CENTER", 0, 40)
			end

			self.IconOverlay = self.Castbar:CreateTexture(nil, "OVERLAY")
			self.IconOverlay:SetPoint("TOPLEFT", self.Castbar.Icon, "TOPLEFT", -1.5, 1)
			self.IconOverlay:SetPoint("BOTTOMRIGHT", self.Castbar.Icon, "BOTTOMRIGHT", 1, -1)
			self.IconOverlay:SetTexture(buttonTex)
			self.IconOverlay:SetVertexColor(1, 1, 1)

			self.IconBackdrop = CreateFrame("Frame", nil, self)
			self.IconBackdrop:SetPoint("TOPLEFT", self.Castbar.Icon, "TOPLEFT", -4, 3)
			self.IconBackdrop:SetPoint("BOTTOMRIGHT", self.Castbar.Icon, "BOTTOMRIGHT", 3, -3.5)
			self.IconBackdrop:SetParent(self.Castbar)
			self.IconBackdrop:SetBackdrop({
			  edgeFile = glowTex, edgeSize = 4,
			  insets = {left = 3, right = 3, top = 3, bottom = 3}
			})
			self.IconBackdrop:SetBackdropColor(0, 0, 0, 0)
			self.IconBackdrop:SetBackdropBorderColor(0, 0, 0, 0.7)
			]]
		end
		
		if unit == "player" and cblatency == true then
			self.Castbar.SafeZone = self.Castbar:CreateTexture(nil, "OVERLAY")
			self.Castbar.SafeZone:SetTexture(normTex)
			self.Castbar.SafeZone:SetVertexColor(0.69, 0.31, 0.31, 0.75)
		end
	end

------------------------------------------------------------------------
--	Raid or Party Leader
------------------------------------------------------------------------

	if not unit or unit == "player" then
		self.Leader = self.Health:CreateTexture(nil, "OVERLAY")
		self.Leader:SetHeight(14)
		self.Leader:SetWidth(14)
		self.Leader:SetPoint("TOPLEFT", 0, 10)
	end

------------------------------------------------------------------------
--	Unitframes Width/Height
------------------------------------------------------------------------

	if unit == "player" or unit == "target" then
		self:SetAttribute("initial-height", 25)
		self:SetAttribute("initial-width", 250)
	elseif  unit == "pet" then
		self:SetAttribute("initial-height", 18)
		self:SetAttribute("initial-width", 120)
        self.Power:Hide()
	elseif unit == "targettarget" then
		self:SetAttribute("initial-height", 18)
		self:SetAttribute("initial-width", 100)
        self.Power:Hide() 
	elseif unit == "targettargettarget" then
		self:SetAttribute("initial-height", 12)
		self:SetAttribute("initial-width", 88) 
		self.Power:Hide()
	elseif unit == "focus" then
		self:SetAttribute("initial-height", 18)
		self:SetAttribute("initial-width", 120)
		self.Power:Hide()
	else
		self:SetAttribute("initial-height", 37)
		self:SetAttribute("initial-width", 249)
		self.Power:Hide()		
	end

------------------------------------------------------------------------
--	RaidIcons
------------------------------------------------------------------------

		self.RaidIcon = self.Health:CreateTexture(nil, "OVERLAY")
		self.RaidIcon:SetHeight(14)
		self.RaidIcon:SetWidth(14)
		self.RaidIcon:SetPoint("TOP", 0, 8)

------------------------------------------------------------------------
--	Background texture and textures border
------------------------------------------------------------------------


	self.FrameBackdrop = CreateFrame("Frame", nil, self)
	self.FrameBackdrop:SetPoint("TOPLEFT", self, "TOPLEFT", -4, 4)
	self.FrameBackdrop:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", 4, -4)
	self.FrameBackdrop:SetFrameStrata("BACKGROUND")
	self.FrameBackdrop:SetBackdrop {
	  edgeFile = glowTex, edgeSize = 5,
	  insets = {left = 3, right = 3, top = 3, bottom = 3}
	}
	self.FrameBackdrop:SetFrameLevel(0)
	self.FrameBackdrop:SetBackdropColor(0, 0, 0, 0)
	self.FrameBackdrop:SetBackdropBorderColor(0, 0, 0)

	self.ThreatFeedbackFrame = self.FrameBackdrop

	self.outsideRangeAlpha = 0.3
	self.inRangeAlpha = 1
	self.SpellRange = true

	self.BarFade = false


	self.PostUpdateHealth = PostUpdateHealth
	self.PreUpdatePower = PreUpdatePower
	self.PostUpdatePower = PostUpdatePower
	self.PostCreateAuraIcon = CreateAura
	self.PostCreateEnchantIcon = CreateAura
	self.PostUpdateAuraIcon = UpdateAura
	self.PostUpdateEnchantIcons = CreateEnchantTimer
	self.PostUpdateThreat = PostUpdateThreat

	self:SetScale(settings.scale)
	if self.Auras then self.Auras:SetScale(settings.scale) end
	if self.Buffs then self.Buffs:SetScale(settings.scale) end
	if self.Debuffs then self.Debuffs:SetScale(settings.scale) end

	HideAura(self)
	return self
end

oUF:RegisterStyle("Tukz", SetStyle)
oUF:SetActiveStyle("Tukz")

local cfg = settings.coords

if epstyle == 1 then
	oUF:Spawn("player", "oUF_Tukz_player"):SetPoint("BOTTOM", UIParent, 0, 135)
	oUF:Spawn("target", "oUF_Tukz_target"):SetPoint("BOTTOM", UIParent, 0, 205)
	oUF:Spawn("pet", "oUF_Tukz_pet"):SetPoint("TOPLEFT", oUF_Tukz_player, "BOTTOMLEFT", 48, -25)
	oUF:Spawn("focus", "oUF_Tukz_focus"):SetPoint("TOPRIGHT", oUF_Tukz_target, "BOTTOMRIGHT", -48, -25)
	oUF:Spawn("targettarget", "oUF_Tukz_targettarget"):SetPoint("LEFT", oUF_Tukz_target, "RIGHT", 10, 0)
	if totot == true then
		oUF:Spawn("targettargettarget", "oUF_Tukz_totot"):SetPoint("TOP", oUF_Tukz_targettarget, "BOTTOM", 0, -1)
	end
else
	oUF:Spawn("player", "oUF_Tukz_player"):SetPoint("BOTTOM", UIParent, -183, 135)
	oUF:Spawn("target", "oUF_Tukz_target"):SetPoint("BOTTOM", UIParent, 183, 135)
	oUF:Spawn("pet", "oUF_Tukz_pet"):SetPoint("TOPLEFT", oUF_Tukz_player, "BOTTOMLEFT", 48, -25)
	oUF:Spawn("focus", "oUF_Tukz_focus"):SetPoint("TOPRIGHT", oUF_Tukz_target, "BOTTOMRIGHT", -48, -25)
	oUF:Spawn("targettarget", "oUF_Tukz_targettarget"):SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 138)
	if totot == true then
		oUF:Spawn("targettargettarget", "oUF_Tukz_totot"):SetPoint("TOP", oUF_Tukz_targettarget, "BOTTOM", 0, -1)
	end
end

local party = oUF:Spawn("header", "oUF_PartyHide")
party:SetAttribute("showParty", false)



